#Create a simulator object
set ns [new Simulator]
set f [open out.tr w]
$ns trace-all $f


#Define a 'finish' procedure
proc finish {} {

        global ns
        $ns flush-trace
        exit 0
}

proc connect {nodeA nodeB} {

    global ns
    $ns duplex-link $nodeA $nodeB 5Mb 2ms DropTail    
}

proc add_ftp {sender dest} {

    global ns

    set tcp_sender [new Agent/TCP]
    set tcp_dest [new Agent/TCPSink]
    
    $ns attach-agent $sender $tcp_sender
    $ns attach-agent $dest $tcp_dest
    $ns connect $tcp_sender $tcp_dest

    set ftp_sender [new Application/FTP]
    set ftp_dest [new Application/FTP]

    $ftp_sender attach-agent $tcp_sender

    return $ftp_sender
}

#Add logic here

#Create 4 nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]

#Connect then
connect $n0 $n2
connect $n1 $n2
connect $n2 $n3

#add ftp
set ftp [add_ftp $n0 $n3]

#send and stop
$ns at 0.5 "$ftp start"
$ns at 9   "$ftp stop"

#Define end time
$ns at 10.0 "finish"

#Run the simulation
$ns run